package com.xuetang9.BBS.dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import com.xuetang9.BBS.dao.CustomDao;
import com.xuetang9.BBS.domain.Customer;

@Repository
public class CustomerDaoImpl  implements CustomDao{

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
/**
 * 获取对象，传入sql语句
 * @return
 *   返回一个对象
 */
	private RowMapper<Customer> getCustomerRowMapper() {
		return new RowMapper<Customer>() {

			@Override
			public Customer mapRow(ResultSet resultSet, int arg1) throws SQLException {
				
				Customer customer = new Customer();
				System.out.println(resultSet.getString("account")+ "   "+resultSet.getString("password"));
				customer.setAccount(resultSet.getString("account"));
				customer.setPassword(resultSet.getString("password"));
				customer.setName(resultSet.getString("name"));
				customer.setHeadUrl(resultSet.getString("headUrl"));
				customer.setGender(resultSet.getString("gender"));
				customer.setBirthday(resultSet.getDate("birthday"));
				customer.setQq(resultSet.getString("qq"));
				customer.setEmail(resultSet.getString("email"));
				customer.setPhone(resultSet.getString("phone"));
				customer.setWechat(resultSet.getString("wechat"));
				customer.setIntroduction(resultSet.getString("introduction"));
				customer.setAutograph(resultSet.getString("autograph"));
				customer.setScore(resultSet.getString("score"));
				customer.setLevel(resultSet.getString("level"));
				return customer;
			}
		};
	}
	
	public int seve(Customer customer) {
		String sql = "insert into customer values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		
		return jdbcTemplate.update(sql,
									customer.getAccount(),
									customer.getPassword(),
									customer.getName(),
									customer.getHeadUrl(),
									customer.getGender(),
									customer.getBirthday(),
									customer.getQq(),
									customer.getEmail(),
									customer.getPhone(),
									customer.getWechat(),
									customer.getIntroduction(),
									customer.getAutograph(),
									customer.getScore(),
									customer.getLevel());	
	                   }

	public Customer findByAccAndPass(String account, String password) {
		String sql = "SELECT * FROM customer where account=? and password=?";
		System.out.println(account+"  "+password);
		List<Customer> list = jdbcTemplate.query(sql, this.getCustomerRowMapper(), account , password );
		if(list.size()==1){
			
			return list.get(0);
		}
		return null;
		
	}
}
